import axios from "axios";

const service = axios.create({
  baseURL: "/api",
  timeout: 5000,
});

//响应拦截器
service.interceptors.response.use(
  (res) => {
    //res : 真实的后端业务响应数据
    // 未设置状态码则默认成功状态
    const code = res.data.code;
    const msg = res.data.msg;
    //强制推送到登陆页面
    if (code === 3001) {
      ElMessage.error(msg);
      removeToken();
      router.push("/oj/login");
      return Promise.reject(new Error(msg));
    } else {
      return Promise.resolve(res.data);
    }
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default service;
